//
//  XHSpeechViewController.m
//  testApp
//
//  Created by ColdMountain on 2017/1/9.
//  Copyright © 2017年 ColdMountain. All rights reserved.
//

#import "XHSpeechViewController.h"
#import <Speech/Speech.h>

@interface XHSpeechViewController ()

@property (nonatomic, strong) AVAudioEngine * auengine;
@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UITextView *text;

@end

@implementation XHSpeechViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.auengine = [[AVAudioEngine alloc]init];
}
- (IBAction)stop:(id)sender
{
    [self.auengine stop];
}
- (IBAction)reset:(id)sender
{
    [self.auengine reset];
}
- (IBAction)pause:(id)sender
{
    [self.auengine pause];
}
- (IBAction)prepare:(id)sender
{
    [self.auengine prepare];
}
- (IBAction)button1:(id)sender
{
    [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
        if (status == SFSpeechRecognizerAuthorizationStatusAuthorized) {
            [self recognizeAudioBuffer];
        }
    }];
}

- (void)recognizeAudioBuffer
{
    self.button1.userInteractionEnabled = NO;
    
    SFSpeechRecognizer *recognize = [[SFSpeechRecognizer alloc] init];
    SFSpeechAudioBufferRecognitionRequest *requestbuffer = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
    
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *error;
    [session setCategory:AVAudioSessionCategoryRecord error:&error];
    if (error) {
        self.text.text = error.description;
        return;
    }
    [session setMode:AVAudioSessionModeMeasurement error:&error];
    if (error) {
        self.text.text = error.description;
        return;
    }
    [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
    if (error) {
        self.text.text = error.description;
        return;
    }
    
    if (!self.auengine.inputNode) {
        self.text.text = @"无输入节点";
        return;
    }
    [self.auengine.inputNode installTapOnBus:0 bufferSize:1024 format:[self.auengine.inputNode outputFormatForBus:0] block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
        if (buffer) {
            [requestbuffer appendAudioPCMBuffer:buffer];
        }
    }];
    
    [recognize recognitionTaskWithRequest:requestbuffer resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        if (result) {
            self.text.text = result.bestTranscription.formattedString;
        }
        if (error) {
            self.text.text = error.description;
        }
    }];
    
    [self.auengine prepare];
    [self.auengine startAndReturnError:&error];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
